use anyhow::Result;
use chat_server::{AppState, config::AppConfig};
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{fmt::Layer, layer::SubscriberExt, util::SubscriberInitExt, Layer as _};    


#[tokio::main]
async fn main() -> Result<()>{
    let layer = Layer::new().with_filter(LevelFilter::INFO);

    tracing_subscriber::registry()
        .with(layer)
        .init();

    let config = AppConfig::load()?; 
    let addr = format!("0.0.0.0:{}",config.server.port);
    tracing::info!("starting server at {}",addr);

    let state = AppState::try_new(config).await?;
    let app = chat_server::get_router(state).await?;
    let listener = tokio::net::TcpListener::bind(&addr).await?;
    axum::serve(listener, app).await?;

    Ok(())
}